// Copyright DustedPixels.com 2008. All rights reserved.

package com.dustedpixels.jasmin.chips;

/**
 * @author micapolos@gmail.com (Michal Pociecha-Los)
 */
public final class Memory4 implements Chip2 {
  public int ADDRESS = 0;
  public final boolean ADDRESS_ACTIVE = false;
  public byte DATA = 0;
  public boolean DATA_ACTIVE = false;
  public boolean WR = false;
  public final boolean WR_ACTIVE = false;
  public boolean REQ = false;
  public final boolean REQ_ACTIVE = false;
  
  private final int mask;
  final byte[] mem;
  
  public Memory4(int addressBits) {
    this.mask = (1 << addressBits) - 1;
    this.mem = new byte[1 << addressBits];
  }
  
  public void update(int cycles) {
    while (cycles-- != 0) {
      DATA_ACTIVE = false;
      if (REQ) {
        if (WR) {
          mem[ADDRESS & mask] = DATA;
        } else {
          DATA = mem[ADDRESS & mask];
          DATA_ACTIVE = true;
        }
      }
    }
  }
}
